package com.ruizhong.PIW.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruizhong.PIW.dao.TWindowMapper;
import com.ruizhong.PIW.entity.TStyle;
import com.ruizhong.PIW.entity.TWindow;
import com.ruizhong.PIW.service.StyleService;
import com.ruizhong.PIW.service.WindowService;
import com.ruizhong.PIW.vo.CalculateVO;
import com.ruizhong.PIW.vo.StyleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class WindowServiceImpl extends ServiceImpl<TWindowMapper, TWindow> implements WindowService {
    @Autowired
    private StyleService styleService;

    @Override
    public void saveWindows(StyleVO styleVO) {
        String styleName = styleVO.getStyleName();
        if (StringUtils.isEmpty(styleName)) {
            throw new RuntimeException("新建样式名称不能为空");
        }
        List<TWindow> list = styleVO.getList();
        if (CollectionUtils.isEmpty(list)) {
            throw new RuntimeException("新建样式的窗口列表为空");
        }
        DecimalFormat df = new DecimalFormat("#.000");
        TStyle tStyle = new TStyle();
        tStyle.setName(styleName);
        tStyle.setMaxIndex(styleVO.getMaxIndex());
        tStyle.setNewNodeNum(styleVO.getNewNodeNum());
        styleService.save(tStyle);
        ArrayList<TWindow> windows = new ArrayList<>();
        for (TWindow tWindow : list) {
            tWindow.setWidth(df.format(Double.parseDouble(tWindow.getWidth()) * 100) + "%");
            tWindow.setHeight(df.format(Double.parseDouble(tWindow.getHeight()) * 100) + "%");
            tWindow.setLeft(df.format(Double.parseDouble(tWindow.getLeft()) * 100) + "%");
            tWindow.setTop(df.format(Double.parseDouble(tWindow.getTop()) * 100) + "%");
            tWindow.setStyle(tStyle.getId());
            windows.add(tWindow);
        }
        boolean b = saveBatch(windows);
        if (!b) {
            throw new RuntimeException("保存失败");
        }
    }

    @Override
    public List<TWindow> getWindowsByStyleId(Integer styleId) {
        List<TWindow> list = lambdaQuery()
                .eq(TWindow::getStyle, styleId)
                .list();
        return list;
    }

    @Override
    @Transactional
    public void submitUpdateStyle(StyleVO styleVO) {
        DecimalFormat df = new DecimalFormat("#.000");
        TStyle tStyle = new TStyle();
        tStyle.setId(styleVO.getStyleId());
        tStyle.setMaxIndex(styleVO.getMaxIndex());
        tStyle.setNewNodeNum(styleVO.getNewNodeNum());
        styleService.updateById(tStyle);
        QueryWrapper<TWindow> windowQueryWrapper = new QueryWrapper<>();
        windowQueryWrapper.lambda().eq(TWindow::getStyle, styleVO.getStyleId());
        remove(windowQueryWrapper);
        List<TWindow> list = styleVO.getList();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        ArrayList<TWindow> windows = new ArrayList<>();
        for (TWindow tWindow : list) {
            tWindow.setWidth(df.format(Double.parseDouble(tWindow.getWidth()) * 100) + "%");
            tWindow.setHeight(df.format(Double.parseDouble(tWindow.getHeight()) * 100) + "%");
            tWindow.setLeft(df.format(Double.parseDouble(tWindow.getLeft()) * 100) + "%");
            tWindow.setTop(df.format(Double.parseDouble(tWindow.getTop()) * 100) + "%");
            tWindow.setStyle(tStyle.getId());
            windows.add(tWindow);
        }
        boolean b = saveBatch(windows);
        if (!b) {
            throw new RuntimeException("修改失败");
        }
    }

    @Override
    public List<TWindow> calculateStyle(CalculateVO calculateVO) {
        Integer windowNum = calculateVO.getWindowNum();
        if (windowNum == null||windowNum == 0) {
            return null;
        }
        Double windowWidth = calculateVO.getWindowWidth();
        Integer columnNum = calculateVO.getColumnNum();
        Integer rowNum = calculateVO.getRowNum();
        Double allWidth = null;
        Double windowIntervalWidth = calculateVO.getWindowIntervalWidth() == null ?0:calculateVO.getWindowIntervalWidth();
        Double canvasWidth = calculateVO.getCanvasWidth();
        Double canvasHeight = calculateVO.getCanvasHeight();
        if ((rowNum == null||rowNum == 0) || (columnNum == null || columnNum == 0)) {
            return null;
        }
        Double windowRealWidth=null;
        if (windowWidth == null) {
            if (columnNum != 0) {
                allWidth = Math.floor(canvasWidth / columnNum);
            } else {
                allWidth = Math.floor(canvasWidth / windowNum);
            }
            windowRealWidth = allWidth - windowIntervalWidth;
        }

        if (windowWidth !=null ){
            windowRealWidth=windowWidth;
            allWidth=windowRealWidth+windowIntervalWidth;
        }
        Double top = (canvasHeight-rowNum*allWidth)/2;
        ArrayList<TWindow> windows = new ArrayList<>();
        //        遍历行
        for (Integer i = 0; i < rowNum; i++) {
//            Double left = windowIntervalWidth / 2;
            Double left=(canvasWidth-columnNum*allWidth)/2;
//            遍历列
            for (Integer integer = 0; integer < columnNum; integer++) {
                TWindow tWindow = new TWindow();
                tWindow.setWidth(String.valueOf(windowRealWidth));
                tWindow.setHeight(String.valueOf(windowRealWidth));
                tWindow.setLeft(String.valueOf(left));
                tWindow.setTop(String.valueOf(top));
                windows.add(tWindow);

                if (windows.size() >= windowNum ){
                    break;
                }
                left += windowRealWidth + windowIntervalWidth;
            }
            if (windows.size() >= windowNum ){
                break;
            }
            top += windowRealWidth + windowIntervalWidth;
        }
        return windows;
    }
}
